Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms

Namespace DeviceDiscoveryApplication

    Partial Public Class Form1
        Inherits Form

        Private deviceList As New List(Of C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.Device)()
        Private deviceType As C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType

        Public Sub New()

            InitializeComponent()
            txtDeviceName.Enabled = False
            cmbDeviceType.Enabled = False

        End Sub

        Private Sub btnExit_Click(ByVal sender As Object, ByVal e As EventArgs) _
                                                            Handles btnExit.Click
            Dispose()
        End Sub

        Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) _
                                    Handles btnCancel.Click

            lstDevices.Items.Clear()
            lstDeviceInfo.Items.Clear()
            txtDeviceName.Text = ""
            cmbDeviceType.SelectedIndex = -1
            cmbDiscover.SelectedIndex = -1

        End Sub

        Private Sub btnOk_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnOk.Click

            Me.Cursor = Cursors.WaitCursor
            Try
                btnCancel.Enabled = False
                DeviceDiscovery()

            Finally
                btnCancel.Enabled = True
                Me.Cursor = Cursors.Default
            End Try

        End Sub

        Private Sub lstDevices_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) _
                                                        Handles lstDevices.SelectedIndexChanged

            lstDeviceInfo.Items.Clear()
            If deviceList IsNot Nothing Then

                For Each device As C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.Device In deviceList

                    If lstDevices.Items.Contains(device.Name) AndAlso device.Name = lstDevices.SelectedItem.ToString() Then
                        With lstDeviceInfo
                            .Items.Add("Name : " & device.Name)
                            .Items.Add("Address : " & device.Address.Address.ToString())
                            .Items.Add("Type : " & device.Type.ToString())
                        End With

                    End If

                Next

            End If

        End Sub

        Private Sub cmbDeviceType_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) _
                                                                Handles cmbDeviceType.SelectedIndexChanged

            Select Case cmbDeviceType.Text

                Case "CellPhone"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.CellPhone
                    Exit Select
                Case "DesktopComputer"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.DesktopComputer
                    Exit Select
                Case "Computer"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.Computer
                    Exit Select
                Case "CordlessPhone"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.CordlessPhone
                    Exit Select
                Case "HandheldComputer"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.HandheldComputer
                    Exit Select
                Case "ImagingCamera"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.ImagingCamera
                    Exit Select
                Case "ImagingDisplay"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.ImagingDisplay
                    Exit Select
                Case "ImagingPrinter"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.ImagingPrinter
                    Exit Select
                Case "ImagingScanner"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.ImagingScanner
                    Exit Select
                Case "LaptopComputer"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.LaptopComputer
                    Exit Select
                Case "PdaComputer"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.PdaComputer
                    Exit Select
                Case "Peripheral"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.Peripheral
                    Exit Select
                Case "PeripheralCardReader"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.PeripheralCardReader
                    Exit Select
                Case "PeripheralDigitizerTablet"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.PeripheralDigitizerTablet
                    Exit Select
                Case "PeripheralGamepad"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.PeripheralGamepad
                    Exit Select
                Case "PeripheralJoystick"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.PeripheralJoystick
                    Exit Select
                Case "PeripheralKeyboard"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.PeripheralKeyboard
                    Exit Select
                Case "PeripheralPointingDevice"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.PeripheralPointingDevice
                    Exit Select
                Case "PeripheralRemoteControl"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.PeripheralRemoteControl
                    Exit Select
                Case "PeripheralSensingDevice"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.PeripheralSensingDevice
                    Exit Select
                Case "Phone"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.Phone
                    Exit Select
                Case "ServerComputer"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.ServerComputer
                    Exit Select
                Case "SmartPhone"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.SmartPhone
                    Exit Select
                Case "WearableComputer"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.WearableComputer
                    Exit Select
                Case "WiredPhone"
                    deviceType = C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.DeviceType.WiredPhone
                    Exit Select

            End Select

        End Sub

        Private Sub cmbDiscover_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) _
                                                            Handles cmbDiscover.SelectedIndexChanged

            lstDeviceInfo.Items.Clear()
            lstDevices.Items.Clear()
            txtDeviceName.Text = ""

            If cmbDiscover.Text <> "" Then

                If cmbDiscover.Text = "Discover All Devices" Then
                    txtDeviceName.Enabled = False
                    cmbDeviceType.Enabled = False
                ElseIf cmbDiscover.Text = "Discover Devices By Type" Then
                    txtDeviceName.Enabled = False
                    cmbDeviceType.Enabled = True
                ElseIf cmbDiscover.Text = "Discover Devices By Name" Then
                    txtDeviceName.Enabled = True
                    cmbDeviceType.Enabled = False
                End If

            End If

        End Sub

        Private Sub DeviceDiscovery()

            lstDeviceInfo.Items.Clear()
            lstDevices.Items.Clear()

            If cmbDiscover.SelectedIndex <> -1 Then

                If cmbDiscover.SelectedItem.ToString() = "Discover All Devices" Then

                    Try
                        txtDeviceName.Enabled = False
                        cmbDeviceType.Enabled = False
                        deviceList.Clear()
                        deviceList = deviceServicesManager1.DiscoverAllDevices()
                        If deviceList IsNot Nothing Then
                            For Each device As C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.Device In deviceList
                                lstDevices.Items.Add(device.Name)
                            Next
                        Else
                            Microsoft.VisualBasic.MsgBox("No device was found.", _
                                             Microsoft.VisualBasic.MsgBoxStyle.Information, "Device Discovery")
                        End If

                    Catch
                        Microsoft.VisualBasic.MsgBox("An unexpected error was raised during device discovery", _
                                        Microsoft.VisualBasic.MsgBoxStyle.Critical, "Device Discovery")
                    End Try

                ElseIf cmbDiscover.SelectedItem.ToString() = "Discover Devices By Type" Then

                    Try
                        txtDeviceName.Enabled = False
                        deviceList.Clear()

                        If cmbDeviceType.SelectedIndex <> -1 Then

                            deviceList = deviceServicesManager1.DiscoverDeviceByType(deviceType)
                            If deviceList IsNot Nothing Then
                                For Each device As C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.Device In deviceList
                                    lstDevices.Items.Add(device.Name)
                                Next
                            Else
                                Microsoft.VisualBasic.MsgBox _
                                ("No device was found.", Microsoft.VisualBasic.MsgBoxStyle.Information _
                                                , "Device Discovery")
                            End If

                        Else
                            Microsoft.VisualBasic.MsgBox("Please select a valid device type", _
                                          Microsoft.VisualBasic.MsgBoxStyle.Information, "Device Discovery")
                            cmbDeviceType.Focus()
                        End If

                    Catch
                        Microsoft.VisualBasic.MsgBox("An unexpected error was raised during device discovery", _
                                        Microsoft.VisualBasic.MsgBoxStyle.Critical, "Device Discovery")
                    End Try

                ElseIf cmbDiscover.SelectedItem.ToString() = "Discover Devices By Name" Then

                    Try
                        deviceList.Clear()
                        cmbDeviceType.Enabled = False
                        If txtDeviceName.Text.Trim() <> "" Then

                            Dim device As C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery.Device = _
                            deviceServicesManager1.DiscoverDeviceByName(txtDeviceName.Text)

                            If device IsNot Nothing Then
                                deviceList.Add(device)
                                lstDevices.Items.Add(device.Name)
                            Else
                                Microsoft.VisualBasic.MsgBox _
                                ("No device was found.", Microsoft.VisualBasic.MsgBoxStyle.Information _
                                                , "Device Discovery")
                            End If

                        Else
                            Microsoft.VisualBasic.MsgBox("Please enter a valid device name", _
                                            Microsoft.VisualBasic.MsgBoxStyle.Information, "Device Discovery")
                            txtDeviceName.Focus()
                        End If

                    Catch
                        Microsoft.VisualBasic.MsgBox("An unexpected error was raised during device discovery", _
                                        Microsoft.VisualBasic.MsgBoxStyle.Critical, "Device Discovery")
                    End Try

                End If

            End If

        End Sub

    End Class

End Namespace
